import Util from '../../utils/util.js';

const app = getApp()
Component({
  properties: {
    searchFilters: Object,
    tabTxt: {
      type: Object,
      observer(newVal, oldVal) {
      }
    },
    initData: Object,
    selectClass: String,
    screenHeight: String,
    screenTop: {
      type: String,
      observer(e) {
        this.setData({
          layerTop: parseInt(e) - 10
        })
      }
    },
    showFilter: Boolean,
    hideMask: {
      type: Boolean,
      value: true
    },
    hideSearch: {
      type: Boolean,
      value: true
    },
    platform: String,
    isfixed: {
      type: Boolean,
      default: false
    },
  },

  data: {
    isMask: true,
    selectLouLevelTwo: [],
  },

  ready() {
    const that = this,
      data = that.data.initData

    wx.getSystemInfo({
      success: (res) => {
        that.setData({
          screenHeight: res.screenHeight,
        })
        console.log(that.data.screenHeight)
      }, fail(err) {
      }
    });
  },

  attached: function () {
    this.init()
  },

  methods: {
    init(e) {
      const that = this,
        tabTxt = that.data.tabTxt

      if (!Util.isEmpty(tabTxt)) {
        let filter = {}
        this.setData({tabTxt: tabTxt})
      }
    },

    // 切换导航按钮
    showSxLayer(e) {
      const that = this;
      // var data = JSON.parse(JSON.stringify(that.data.tabTxt));
      var data = that.data.tabTxt
      var index = e.currentTarget.dataset.index;

      var newTabTxt = data.map(function (e) {
        e.active = false;
        return e;
      });

      newTabTxt[index].active = !that.data.tabTxt[index].active;
      this.setData({
        tabTxt: data,
        isMask: false
      })

    },

    selectTwo(e) {
      const index = e.currentTarget.dataset.index,
        index2 = e.currentTarget.dataset.index2,
        index1 = e.currentTarget.dataset.index1

      let tabTxt = this.data.tabTxt
      if (tabTxt[index1].child[index2].child[index].id != -1) {
        tabTxt[index1].child[index2].child[0].active = false
        tabTxt[index1].child[index2].child[index].active = !tabTxt[index1].child[index2].child[index].active
      } else {
        tabTxt[index1].child[index2].child.forEach(e1 => {
          e1.active = false
        })
        tabTxt[index1].child[index2].child[index].active = true
      }
      this.setData({
        tabTxt: tabTxt
      })
    },

    submitConfirm(e) {
      const index = e.currentTarget.dataset.index
      let tabTxt = this.data.tabTxt

      this.triggerEvent('changeFilterFun', {
        start_time: this.data.start_time || "",
        end_time: this.data.end_time || "",
      });

      this.closeMask();
    },

    clearFilter(e) {
      const index = e.currentTarget.dataset.index
      let tabTxt = this.data.tabTxt

      for (const k in tabTxt[index].child) {
        if (Util.isEmpty(tabTxt[index].child[k]._child)) {
          continue;
        }
        tabTxt[index].child[k]._child.forEach(e1 => {
          e1.active = false
        })
        tabTxt[index].child.forEach(e1 => {
          e1.active = false;
        })
        tabTxt[index].child[0].active = true;
      }

      if (index === 2) {
        this.setData({
          selectLouLevelTwo: []
        })

        this.triggerEvent('changeFilterFun', {[tabTxt[index].name]: -1});
      }
      this.setData({
        tabTxt: tabTxt
      })

    },
    selectSingle: function (e) {
      const index = e.currentTarget.dataset.index,
        index1 = e.currentTarget.dataset.index1;

      let tabTxt = this.data.tabTxt
      tabTxt[index1].active = false

      if (tabTxt[index1].name == 'shop_id') {
        tabTxt[1].default_val = 0;
        tabTxt[1].subtitle = "";
      }

      if (tabTxt[index1].level == 4) {
        tabTxt[index1].default_val = index;
        for (const idx in tabTxt[index1].child) {
          if (idx == index) {
            tabTxt[index1].subtitle = tabTxt[index1].child[idx].title || ""
          }
        }
      } else {
        tabTxt[index1].child.forEach(e1 => {
          e1.active = false
        })
        tabTxt[index1].child[index].active = true;
        if (!Util.isEmpty(tabTxt[index1].range_key)) {
          tabTxt[index1].default_val = tabTxt[index1].child[index].id
        }
        tabTxt[index1].subtitle = tabTxt[index1].child[index].id != -1 ? tabTxt[index1].child[index].title : ""
      }

      console.log(tabTxt)

      console.log(111)

      //餐厅对应的餐厅
      if (index1 == 0) {
        // tabTxt[1].default_val = 0;
        tabTxt[2].default_val = 0;
      }

      this.setData({
        tabTxt: tabTxt
      })
      this.triggerEvent('changeFilterFun', this.initFilter());

      this.closeMask();
    },

    initFilter() { //初始化条件

      const tabTxt = this.data.tabTxt;
      let filter = [];

      tabTxt.map((e1, idx) => {
        if (e1.level == 1) {
          e1.child.map((e2, idx1) => {
            if (e2.active) {
              e1.subtitle = e2.title
              filter[e1.name] = e2.id
            }
          })

          return ;
        }else if (parseInt(e1.default_val || 0) > 0) {
          filter[e1.name] = e1.default_val

          if (e1.range_key && e1.name != 'city_id') {
            e1.child.map((e2, idx1) => {
              if (e2.check) {
                e1.subtitle = e2.title
              }
            })
          } else {
            e1.subtitle = e1.child[e1.default_val] || ''
          }
        } else if (e1.default_val == 0) {
          filter[e1.name] = 0
        }

      })

      console.log(filter)
      return filter;
    },

    changeArea(e) {
      const that = this,
        index1 = e.currentTarget.dataset.index1,
        index2 = e.currentTarget.dataset.index2,
        index3 = e.currentTarget.dataset.index3

      let tabTxt = this.data.tabTxt
      tabTxt[index1].child.forEach(e1 => {
        e1.active = false
      })
      tabTxt[index1].child[index2].active = true;
      if (tabTxt[index1].child[index2].id == -1) {
        tabTxt[index1].subtitle = ""
        tabTxt[index1].child.forEach(e1 => {
          e1.active = false
          if (Util.isEmpty(e1._child)) {
            return;
          }
          e1._child.forEach(e2 => {
            e2.active = false;
          })
        })

        this.setData({
          tabTxt: tabTxt,
          selectLouLevelTwo: []
        })

        let filter = this.initFilter();
        if (!Util.isEmpty(filter.city_id)) {
          delete filter['city_id']
        }

        if (!Util.isEmpty(filter.provinceIds)) {
          delete filter['provinceIds']
        }
        // this.triggerEvent('changeFilterFun', {city_id: [], provinceIds: []});
        this.triggerEvent('changeFilterFun', filter);
        this.closeMask();
        return
      }

      let selectLouLevelTwo = this.data.selectLouLevelTwo || {}
      let area_id = 0

      if (index3 === 0) { //二级
        tabTxt[index1].child[index2]._child[index3].active = !tabTxt[index1].child[index2]._child[index3].active;
        tabTxt[index1].child[index2]._child.forEach((e1, idx1) => {
          if (!Util.isEmpty(selectLouLevelTwo[index1 + '_' + index2 + '_' + idx1])) {
            delete selectLouLevelTwo[index1 + '_' + index2 + '_' + idx1];
          }

          if (e1.id == -1)
            return;
          e1.active = false;
        })
        selectLouLevelTwo[index1 + '_' + index2 + '_0'] = tabTxt[index1].child[index2]._child[index3];

        this.setData({selectLouLevelTwo: selectLouLevelTwo})
      } else if (index3) {
        tabTxt[index1].child[index2]._child[0].active = false
        tabTxt[index1].child[index2]._child[index3].active = !tabTxt[index1].child[index2]._child[index3].active;

        if (tabTxt[index1].child[index2]._child[index3].active) {
          selectLouLevelTwo[index1 + '_' + index2 + '_' + index3] = tabTxt[index1].child[index2]._child[index3];
        } else {
          delete selectLouLevelTwo[index1 + '_' + index2 + '_' + index3];
        }

        if (selectLouLevelTwo[index1 + '_' + index2 + '_0']) {
          delete selectLouLevelTwo[index1 + '_' + index2 + '_0'];
        }

        this.setData({selectLouLevelTwo: selectLouLevelTwo})
      }

      this.setData({
        tabTxt: tabTxt
      })
    },

    //筛选楼栋
    confirmLouAddress(e) {
      const that = this,
        louAddress = this.data.selectLouLevelTwo;

      let provinceIds = [],
        cityIds = [];

      if (!Util.isEmpty(louAddress)) {
        for (const k in louAddress) {
          if (louAddress[k].id == -1) {
            provinceIds.push(louAddress[k].pid)
          } else {
            cityIds.push(louAddress[k].id)
          }
        }
      }

      if (cityIds.length > 0 || provinceIds.length > 0) {
        let filter = this.initFilter();
        if (!Util.isEmpty(cityIds)) {
          filter['city_id'] = cityIds;
        }

        if (!Util.isEmpty(provinceIds)) {
          filter['provinceIds'] = provinceIds;
        }

        /*
        this.triggerEvent('changeFilterFun', {
          city_id: cityIds,
          provinceIds: provinceIds,
        });
        */
        this.triggerEvent('changeFilterFun', filter);
      }

      this.closeMask()
      return;
    },

    bindDateChange(e) {
      const that = this,
        key = e.currentTarget.dataset.name
      let date = e.detail.value
      that.setData({
        [key]: date
      })
    },

    // 关闭浮层
    closeMask(e) {
      let tabTxt = this.data.tabTxt;
      if (!tabTxt) return false;
      tabTxt = tabTxt.map(function (e) {
        e.active = false;
        return e;
      });

      this.setData({
        isMask: true,
        tabTxt: tabTxt
      });
    }
  }
});
